<?php

/**
 * Dashboard component
 *
 * PHP versions 5
 * CAKEPHP versions 2.x
 * 
 * Green CMS - Content Management System and Framework Powerfull by Cakephp
 * Copyright 2012, GREEN GLOBAL CO., LTD (toancauxanh.vn)
 * 
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @author        Technology Lab No.I <tech1@toancauxanh.vn>
 * @link          
 * @package       User.Controller.Component
 * @since         Green v 1.0
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('Component', 'Controller');
App::uses('HttpSocket', 'Network/Http');

class DashboardComponent extends Component {

	/**
	 * Other components utilized by Dashboard
	 *
	 * @var array
	 */
	public $components = array();

	/**
	 * Get controller
	 * 
	 * @return Controller
	 */
	public function controller() {
		return $this->_Collection->getController();
	}

	/**
	 * Check id exists
	 * 
	 * @param mixed $result
	 * 
	 * @return void
	 */
	public function error($throwError) {
		if ($throwError) {
			throw new NotFoundException(__('404 Not Found'));
		}
	}

	/**
	 * Parse Redirect
	 * 
	 * @param array $url
	 * @param boolean $continue
	 * @param boolean $back
	 * @return void
	 */
	function redirect($url, $direct = false, $back = false) {
		$controller = $this->controller();
		$_url = null;
		if (!empty($controller->request->query['continue'])) {
			$_url = $controller->request->query['continue'];
		}
		if ($direct && $_url) {
			$url = $_url;
		} elseif (!($back && !$_url)) {
			if (!$back) {
				$_url = $controller->request->here(false);
			}
			if (!isset($url['?'])) {
				$url['?'] = array();
			}
			$url['?'] = array_merge($url['?'], array(
				'continue' => $_url));
		}
		$controller->redirect($url);
	}

	public function removeScheme($url) {
		return preg_replace('/^https?:\/\//', '', trim($url));
	}

	public function checkPosition($keyword, $url, $lang = 'vi') {
		$url = $this->removeScheme($url);
		if (empty($keyword) || empty($url)) {
			return false;
		}
		$apiURL = 'http://www.google.com.vn/search';
		$userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0';
		$HttpSocket = new HttpSocket(array(
					'request' => array(
						'header' => array(
							'Connection' => 'close',
							'User-Agent' => $userAgent
						),
					)
				));
		$Doc = new DOMDocument();
		$pos = $realPos = 0;
		for ($i = 1; $i <= 10; $i++) {

			$html = $HttpSocket->get($apiURL, array(
				'start' => $i,
				'q' => $keyword,
				'hl' => $lang));
			@$Doc->loadHTML('<meta charset="utf-8" />' . $html);
			$rso = $Doc->getElementById('rso');
			$nodes = $rso->getElementsByTagName('h3');
			foreach ($nodes as $node) {
				if ($node->getAttribute('class') != 'r') {
					continue;
				}
				$pos++;
				$link = $node->getElementsByTagName('a')->item(0);
				//echo '<div style="font-size:0.7em">' . $link->getAttribute('href') . '<br /></div>'; // Show all links
				if (strstr($this->removeScheme($link->getAttribute('href')), $url)) {
					$realPos = $pos;
					break;
				}
			}
		}
		return $realPos;
	}

}

?>
